package Web.Servlet;

import Domain.Notice;
import Domain.User;
import Service.Impl.NoticeServiceImpl;
import Service.Impl.UserServiceImpl;
import Service.NoticeService;
import Service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/NoticeServlet")
public class NoticeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");

        if(action.equals("IntoNotice")){
            String uid = request.getParameter("uid");
            String nid = request.getParameter("nid");

            NoticeService service = new NoticeServiceImpl();
            List<Notice> list = service.FindNoticeById(Integer.parseInt(nid));
            Notice notice = list.get(0);

            if(!uid.equals("")){
                UserService service1 = new UserServiceImpl();
                List<User> list1 = service1.findUserById(Integer.parseInt(uid));
                User user = list1.get(0);

                request.setAttribute("user",user);
            }

            request.setAttribute("notice",notice);
            request.getRequestDispatcher("Notice/NoticePage.jsp").forward(request,response);
        }

        if(action.equals("BackToIndex")){
            String uid = request.getParameter("uid");

            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(uid));
            User user = list.get(0);
            request.setAttribute("user",user);
            request.getRequestDispatcher("Index.jsp").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
